package com.cndw.kungfu.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.cndw.kungfu.comm.tencent.LogFieldType;
import com.cndw.kungfu.comm.tencent.QQDC;
import com.cndw.kungfu.config.Constants;
import com.cndw.kungfu.config.ConstantsLevel;
import com.cndw.kungfu.domain.Player;
import com.cndw.kungfu.ext.LanguageLib;
import com.cndw.kungfu.model.Context;
import com.cndw.kungfu.model.general.RankingListManager;
import com.cndw.kungfu.vo.rankingvo.PlayerRankingVO;
import com.cndw.rpg.annotation.Controller;
import com.cndw.rpg.framework.ResponseMessage;
import com.cndw.rpg.framework.Session;

/**
 * 排行版
 * 
 * 
 * @author <a href="mailto:whz-work@163.com">汪洪州</a>
 * 
 * @since 1.0 Create on 2011-8-4
 */
@Controller
public class RankingListController {

	@Resource
	private RankingListManager rankingListManager;

	/**
	 * 获取个人排行版
	 * 
	 * @param session
	 * @return
	 */
	public ResponseMessage getPlayerRanking(Session session) {

		Player player = Context.getPlayerServer().getByPlayerId(session.getUid());

		if (player.getLevel() < ConstantsLevel.lv08Ranking) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("ranking.level.err", ConstantsLevel.lv08Ranking));
		}
		QQDC.getInstance().logClick(player, LogFieldType.order);

		return ResponseMessage.getSuccMessage(rankingListManager.getPlayerRanking(session.getUid()));
	}

	/**
	 * 获取排行榜
	 * 
	 * @param session
	 * @param paramMap
	 *            {page,pageSize}
	 * @param type
	 *            1：等级，2：战力，3：徒弟，4：帮派，5：宝鉴，6：千层塔
	 * @return
	 */
	public ResponseMessage getRankingList(Session session, Map<String, Object> paramMap, int type) {

		Player player = Context.getPlayerServer().getByPlayerId(session.getUid());

		if (player.getLevel() < ConstantsLevel.lv08Ranking) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("ranking.level.err", ConstantsLevel.lv08Ranking));
		}

		return getRanking(paramMap, type,session.getUid());
	}

	public ResponseMessage getRanking(Map<String, Object> paramMap, int type,long uid) {

		int page = paramMap.containsKey("page") ? Integer.parseInt(paramMap.get("page").toString()) : 1;
		int pageSize = paramMap.containsKey("pageSize") ? Integer.parseInt(paramMap.get("pageSize").toString()) : Constants.PAGE_SIZE;

		List<?> rankling = new ArrayList<Object>(1);
		switch (type) {
		case 1: // 等级
			rankling = rankingListManager.getLevelRanking();
			break;
		case 2:// 战斗力
			rankling = rankingListManager.getAttackRanking();
			break;
		case 3:// 徒弟
			rankling = rankingListManager.getDiscipleRanking();
			break;
		case 4:// 帮派
			rankling = rankingListManager.getUnionRanking();
			break;
		case 5:// 图鉴
			rankling = rankingListManager.getCollectRanking();
			break;
		 case 6:// 千层塔
			 rankling = rankingListManager.getTowerRanking();
			 break;
		}

		int[] paging = getPaging(pageSize, page, rankling.size());
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("rankingSize", rankling.size());
		resultMap.put("rankingList", rankling.subList(paging[0], paging[1]));
		resultMap.put("nextUpdateTime", rankingListManager.getNextUpdateTime());
		if (type == 4) {
			PlayerRankingVO myRanking = rankingListManager.getPlayerRanking(uid);
			if (myRanking != null) {
				resultMap.put("unionRank", myRanking.getUnionRank());
			}
		}

		return ResponseMessage.getSuccMessage(resultMap);
	}

	private int[] getPaging(int pageSize, int page, int size) {

		int[] paging = new int[] { 0, 0 };
		int pageCounts = (int) Math.ceil((double) size / pageSize);
		if (page < 0 || page > pageCounts) {
			return paging;
		}

		paging[0] = (page - 1) * pageSize;
		paging[1] = paging[0] + pageSize;

		if (paging[1] > size) {
			paging[1] = size;
		}

		return paging;
	}
}
